clear all
set more off
set mem 10000000
set matsize 10000

*****************************
*** BUILD ECONOMIC CENSUS ***
*****************************

** Set file paths
do "$path_code/paths.do"

********************************************************************************
********************************************************************************

** Step 1: READ IN 1990 DATA
{
//***read in delimited data ec3dt02c.txt - ec3dt09c.txt***//
//save in the same folder as c3dt02c.dta - ec3dt09c.dta//

cd "$ec/1990/raw"

foreach i of numlist 2/9 11/31 33 {
	infile using "$ec/1990/dictionaries/dictionary`i'.dct", clear
	
	* change all strings to upper case
	ds, has(type string) 
	foreach v in `r(varlist)' { 
		replace `v' = trim(itrim(upper(`v')))
	} 
	
	// separate village and ward. Village is for rural and ward is for urban
	gen village_cde = .
	gen ward_code = .
	replace village_cde = villageward if sector == 1
	replace ward_code = villageward if sector == 2
	drop villageward

	// separate tehsil and town. Tehsil is for rural and town is for urban
	gen tehsil_code = .
	gen town_code = .
	replace tehsil_code = tehsiltown if sector == 1
	replace town_code = tehsiltown if sector == 2
	drop tehsiltown

	//recode status of premises into dummy//
	generate float premises_dummy = 0
	replace premises_dummy = 1 if premises == 2
	replace premises_dummy =0 if premises == 1

	//recode operation nature and generate a new dummy variable perennial//
	generate float perennial_dummy = 0
	replace perennial = 1 if operationnature == 1


	//***generate a new variable state (state name), recode state code into state***//
	generate strL state = "."
	format %25s state
	label variable state "state name *Jammu & Kashmir:census not conducted *Lakshadweep:data not available"

	replace state = "Andhra Pradesh" if state_code == 2
	replace state = "Arunachal Pradesh" if state_code == 3
	replace state = "Assam" if state_code == 4
	replace state = "Bihar" if state_code == 5
	replace state = "Goa" if state_code == 6
	replace state = "Gujarat" if state_code == 7
	replace state = "Haryana" if state_code == 8
	replace state = "Himachal Pradesh" if state_code == 9
	replace state = "Jammu & Kashmir" if state_code == 10
	replace state = "Karnataka" if state_code == 11
	replace state = "Kerala" if state_code == 12
	replace state = "Madhya Pradesh" if state_code == 13
	replace state = "Maharashtra" if state_code == 14
	replace state = "Manipur" if state_code == 15
	replace state = "Meghalaya" if state_code == 16
	replace state = "Mizoram" if state_code == 17
	replace state = "Nagaland" if state_code == 18
	replace state = "Orissa" if state_code == 19
	replace state = "Punjab" if state_code == 20
	replace state = "Rajasthan" if state_code == 21
	replace state = "Sikkim" if state_code == 22
	replace state = "Tamil Nadu" if state_code == 23
	replace state = "Tripura" if state_code == 24
	replace state = "Uttar Pradesh" if state_code == 25
	replace state = "West Bengal" if state_code == 26
	replace state = "Andaman & Nicobar" if state_code == 27
	replace state = "Chandigarh" if state_code == 28
	replace state = "Dadra & Nagar Haveli" if state_code == 29
	replace state = "Daman & Diu" if state_code == 30
	replace state = "Delhi" if state_code == 31
	replace state = "Lakshadweep" if state_code == 32
	replace state = "Pondicherry" if state_code == 33

	replace state = upper(state)

	//***label variables***//
	label data "This file combines 30 data sets from ec3dt02c-33c. No data set ec3dt10c and ec3dt32c"
	label variable sector "1-R, 2-U"
	label variable state_code "02-33"
	label variable district_code "district code 0-63"
	label variable admin_div "Tehsil/taluka/ps Dev., Block/Circle etc"
	label variable dev_block "Development Block"
	label variable villagetown "Village/Town"
	label variable wmhcode "Ward/Mohalla/Hamlet-code" 
	label variable enumblock "Enumeration Block No." 
	label variable enumblock2 "Additional Enumeration Blk No." 
	label variable enterprise "Srl. No. of enterprise(with premises)"
	label variable enterprise2 "Srl. No. of Enterprise (without Premises)"
	label variable nic_code "Activity Code"
	label variable broadactivity "Major Activity Code: 1-15"
	label variable agriculture_dummy "1-Agr,2-Non-agr"
	label variable operationnature "1-Perennial,2-seasonal"
	label variable ownership "1-private,2-coop,3-public"
	label variable socialgroup "1-SC,2-ST,3-Gen"
	label variable powerfuel "0-9"
	label variable totalmale "Males(including unpaid & hired) *all zeros for no entry" 
	label variable totalfemale "Female(including unpaid& hired)" 
	label variable total_of_total "Total (males and females)" 
	label variable hired_male "Males (hired)" 
	label variable hired_female "Females (hired)" 
	label variable hired_total "Total(males & females hired)" 
	label variable directory "1-Dir.Est 2-Non Dir.Est 3-OAE"
	label variable oaeest "1-OAE 2-EST"
	label variable bigcity "Big city code  " 
	label variable employsize1 "Total employment size - class code (Type- I): 1-7"
	label variable employsize2 "Total employment size - class code (Type-II): 1-14"
	label variable nature_econ "Nature of EconomicActivity code" 
	describe


	//generate new variables and recode: district name

	generate strL district_name = "."
	label variable district_name "district name"

	replace district_name = "ANDHRA PRADESH" if (state_code == 2)
	replace district_name = "SRIKAKULAM" if (state_code == 2) & (district_code ==1)
	replace district_name = "VIJAINAGARAM" if (state_code == 2) & (district_code ==2)
	replace district_name = "VISAKHAPATNAM" if (state_code == 2) & (district_code ==3)
	replace district_name = "EAST GODAVARI" if (state_code == 2) & (district_code ==4)
	replace district_name = "WEST GODAVARI" if (state_code == 2) & (district_code ==5)
	replace district_name = "KRISHANA" if (state_code == 2) & (district_code ==6)
	replace district_name = "GUNTUR" if (state_code == 2) & (district_code ==7)
	replace district_name = "PRAKASAM" if (state_code == 2) & (district_code ==8)
	replace district_name = "NELLORE" if (state_code == 2) & (district_code ==9)
	replace district_name = "CHITTOOR" if (state_code == 2) & (district_code ==10)
	replace district_name = "CUDDAPAH" if (state_code == 2) & (district_code ==11)
	replace district_name = "ANANTAPUR" if (state_code == 2) & (district_code ==12)
	replace district_name = "KURNOOL" if (state_code == 2) & (district_code ==13)
	replace district_name = "MAHBUBNAGAR" if (state_code == 2) & (district_code ==14)
	replace district_name = "RANGAREDDY" if (state_code == 2) & (district_code ==15)
	replace district_name = "HYDERABAD" if (state_code == 2) & (district_code ==16)
	replace district_name = "MEDAK" if (state_code == 2) & (district_code ==17)
	replace district_name = "NIZAMABAD" if (state_code == 2) & (district_code ==18)
	replace district_name = "ADILABAD" if (state_code == 2) & (district_code ==19)
	replace district_name = "KARIMNAGAR" if (state_code == 2) & (district_code ==20)
	replace district_name = "WARANGAL" if (state_code == 2) & (district_code ==21)
	replace district_name = "KHAMMAM" if (state_code == 2) & (district_code ==22)
	replace district_name = "NALGONDA" if (state_code == 2) & (district_code ==23)
	replace district_name = "ANDHRA PRADESH" if (state_code == 3)
	replace district_name = "TAWANG" if (state_code == 3) & (district_code ==1)
	replace district_name = "WEST KOMENG" if (state_code == 3) & (district_code ==2)
	replace district_name = "EAST KEMENG" if (state_code == 3) & (district_code ==3)
	replace district_name = "LOWER SUBANSIRI" if (state_code == 3) & (district_code ==4)
	replace district_name = "UPPER SUBANSIRI" if (state_code == 3) & (district_code ==5)
	replace district_name = "WEST SIANG" if (state_code == 3) & (district_code ==6)
	replace district_name = "EAST SIANG" if (state_code == 3) & (district_code ==7)
	replace district_name = "DIBANG VALLEY" if (state_code == 3) & (district_code ==8)
	replace district_name = "LOHIT" if (state_code == 3) & (district_code ==9)
	replace district_name = "CHANGLANG" if (state_code == 3) & (district_code ==10)
	replace district_name = "TIRAP" if (state_code == 3) & (district_code ==11)
	replace district_name = "ASSAM" if (state_code == 4)
	replace district_name = "DHUBRI" if (state_code == 4) & (district_code ==1)
	replace district_name = "KOKRAJPUR" if (state_code == 4) & (district_code ==2)
	replace district_name = "BONGAIGAON" if (state_code == 4) & (district_code ==3)
	replace district_name = "GOALPARA" if (state_code == 4) & (district_code ==4)
	replace district_name = "BARPETA" if (state_code == 4) & (district_code ==5)
	replace district_name = "NALBARI" if (state_code == 4) & (district_code ==6)
	replace district_name = "KAMRUP" if (state_code == 4) & (district_code ==7)
	replace district_name = "DARRANG" if (state_code == 4) & (district_code ==8)
	replace district_name = "SONITPUR" if (state_code == 4) & (district_code ==9)
	replace district_name = "LAKHIMPUR" if (state_code == 4) & (district_code ==10)
	replace district_name = "DHEMAJI" if (state_code == 4) & (district_code ==11)
	replace district_name = "MARIGAON" if (state_code == 4) & (district_code ==12)
	replace district_name = "NAGAON" if (state_code == 4) & (district_code ==13)
	replace district_name = "GOLAGHAT" if (state_code == 4) & (district_code ==14)
	replace district_name = "JORHAT" if (state_code == 4) & (district_code ==15)
	replace district_name = "SIBSAGAR" if (state_code == 4) & (district_code ==16)
	replace district_name = "DIBRUGARH" if (state_code == 4) & (district_code ==17)
	replace district_name = "TINSUKIA" if (state_code == 4) & (district_code ==18)
	replace district_name = "KARBIANGLONG" if (state_code == 4) & (district_code ==19)
	replace district_name = "NORTH CACHAR HILLS" if (state_code == 4) & (district_code ==20)
	replace district_name = "KARIMGANJ" if (state_code == 4) & (district_code ==21)
	replace district_name = "HAILAKANDI" if (state_code == 4) & (district_code ==22)
	replace district_name = "CACHAR" if (state_code == 4) & (district_code ==23)
	replace district_name = "BIHAR" if (state_code == 5)
	replace district_name = "PATNA" if (state_code == 5) & (district_code ==1)
	replace district_name = "NLANDA" if (state_code == 5) & (district_code ==2)
	replace district_name = "BHOIPUR" if (state_code == 5) & (district_code ==3)
	replace district_name = "ROLRIAS" if (state_code == 5) & (district_code ==4)
	replace district_name = "AURANGABAD" if (state_code == 5) & (district_code ==5)
	replace district_name = "JEHANABAD" if (state_code == 5) & (district_code ==6)
	replace district_name = "GAYA" if (state_code == 5) & (district_code ==7)
	replace district_name = "NAWADA" if (state_code == 5) & (district_code ==8)
	replace district_name = "SARAN" if (state_code == 5) & (district_code ==9)
	replace district_name = "SIWAN" if (state_code == 5) & (district_code ==10)
	replace district_name = "GOPALGANJ" if (state_code == 5) & (district_code ==11)
	replace district_name = "PASHCHIM CHAMPARAN" if (state_code == 5) & (district_code ==12)
	replace district_name = "PURBA CHAMPARAN" if (state_code == 5) & (district_code ==13)
	replace district_name = "SHAMARHI" if (state_code == 5) & (district_code ==14)
	replace district_name = "MUZAFFARPUR" if (state_code == 5) & (district_code ==15)
	replace district_name = "VAISHALI" if (state_code == 5) & (district_code ==16)
	replace district_name = "BEGUSARAI" if (state_code == 5) & (district_code ==17)
	replace district_name = "SAMASTIPUR" if (state_code == 5) & (district_code ==18)
	replace district_name = "DARBHANGA" if (state_code == 5) & (district_code ==19)
	replace district_name = "MADHUBANI" if (state_code == 5) & (district_code ==20)
	replace district_name = "SAHARSA" if (state_code == 5) & (district_code ==21)
	replace district_name = "MADHEPURA" if (state_code == 5) & (district_code ==22)
	replace district_name = "PURNIA" if (state_code == 5) & (district_code ==23)
	replace district_name = "KATIHAR" if (state_code == 5) & (district_code ==24)
	replace district_name = "KHAGARIA" if (state_code == 5) & (district_code ==25)
	replace district_name = "MUNGER" if (state_code == 5) & (district_code ==26)
	replace district_name = "BHAGALPUR" if (state_code == 5) & (district_code ==27)
	replace district_name = "GODDA" if (state_code == 5) & (district_code ==28)
	replace district_name = "SAHIBGANJ" if (state_code == 5) & (district_code ==29)
	replace district_name = "DUMKA" if (state_code == 5) & (district_code ==30)
	replace district_name = "DEOGHAR" if (state_code == 5) & (district_code ==31)
	replace district_name = "DHANBAD" if (state_code == 5) & (district_code ==32)
	replace district_name = "GIRIDIH" if (state_code == 5) & (district_code ==33)
	replace district_name = "HAZARIBAG" if (state_code == 5) & (district_code ==34)
	replace district_name = "PALAMU" if (state_code == 5) & (district_code ==35)
	replace district_name = "LOHARDAGA" if (state_code == 5) & (district_code ==36)
	replace district_name = "GUMLA" if (state_code == 5) & (district_code ==37)
	replace district_name = "RANCHI" if (state_code == 5) & (district_code ==38)
	replace district_name = "PURBI SINGHBHUM" if (state_code == 5) & (district_code ==39)
	replace district_name = "PASCHCHIMI SINGHBHUM" if (state_code == 5) & (district_code ==40)
	replace district_name = "ARARIA" if (state_code == 5) & (district_code ==41)
	replace district_name = "KISHANGANJ" if (state_code == 5) & (district_code ==42)
	replace district_name = "NORTH GOA" if (state_code == 6) & (district_code ==1)
	replace district_name = "SOUTH GOA" if (state_code == 6) & (district_code ==2)
	replace district_name = "GUJARAT" if (state_code == 7)
	replace district_name = "JAMNAGAR" if (state_code == 7) & (district_code ==1)
	replace district_name = "RAJKOT" if (state_code == 7) & (district_code ==2)
	replace district_name = "SURENDRA NAGAR" if (state_code == 7) & (district_code ==3)
	replace district_name = "BHAVNAGAR" if (state_code == 7) & (district_code ==4)
	replace district_name = "AMRELI" if (state_code == 7) & (district_code ==5)
	replace district_name = "JUNAGADH" if (state_code == 7) & (district_code ==6)
	replace district_name = "KACHCHH" if (state_code == 7) & (district_code ==7)
	replace district_name = "BANAS KANTHA" if (state_code == 7) & (district_code ==8)
	replace district_name = "SABAR KANTHA" if (state_code == 7) & (district_code ==9)
	replace district_name = "MAHESANA" if (state_code == 7) & (district_code ==10)
	replace district_name = "GANDHI NAGAR" if (state_code == 7) & (district_code ==11)
	replace district_name = "AHMADABAD" if (state_code == 7) & (district_code ==12)
	replace district_name = "KHEDA" if (state_code == 7) & (district_code ==13)
	replace district_name = "PANCH MAHALAS" if (state_code == 7) & (district_code ==14)
	replace district_name = "VADODARA" if (state_code == 7) & (district_code ==15)
	replace district_name = "BHARUCH" if (state_code == 7) & (district_code ==16)
	replace district_name = "SURAT" if (state_code == 7) & (district_code ==17)
	replace district_name = "VALSAD" if (state_code == 7) & (district_code ==18)
	replace district_name = "THE DANGS" if (state_code == 7) & (district_code ==19)
	replace district_name = "HARAYANA" if (state_code == 8)
	replace district_name = "AMBALA" if (state_code == 8) & (district_code ==1)
	replace district_name = "YAMUNA NAGAR" if (state_code == 8) & (district_code ==2)
	replace district_name = "KURUKSHETRA" if (state_code == 8) & (district_code ==3)
	replace district_name = "KAITHAL" if (state_code == 8) & (district_code ==4)
	replace district_name = "KARNAL" if (state_code == 8) & (district_code ==5)
	replace district_name = "PANIPAT" if (state_code == 8) & (district_code ==6)
	replace district_name = "SONIPAT" if (state_code == 8) & (district_code ==7)
	replace district_name = "ROHTAK" if (state_code == 8) & (district_code ==8)
	replace district_name = "FARIDABAD" if (state_code == 8) & (district_code ==9)
	replace district_name = "GURGAON" if (state_code == 8) & (district_code ==10)
	replace district_name = "REWARI" if (state_code == 8) & (district_code ==11)
	replace district_name = "MAHENDRAGARH" if (state_code == 8) & (district_code ==12)
	replace district_name = "BHIWANI" if (state_code == 8) & (district_code ==13)
	replace district_name = "JIND" if (state_code == 8) & (district_code ==14)
	replace district_name = "HISAR" if (state_code == 8) & (district_code ==15)
	replace district_name = "SIRSA" if (state_code == 8) & (district_code ==16)
	replace district_name = "HIMACHAL PRADESH" if (state_code == 9)
	replace district_name = "CHAMBA" if (state_code == 9) & (district_code ==1)
	replace district_name = "KANGRA" if (state_code == 9) & (district_code ==2)
	replace district_name = "HAMIRPUR" if (state_code == 9) & (district_code ==3)
	replace district_name = "UNA" if (state_code == 9) & (district_code ==4)
	replace district_name = "BILASHPUR" if (state_code == 9) & (district_code ==5)
	replace district_name = "MANDI" if (state_code == 9) & (district_code ==6)
	replace district_name = "KULU" if (state_code == 9) & (district_code ==7)
	replace district_name = "LAHOL & SPITI" if (state_code == 9) & (district_code ==8)
	replace district_name = "SHIMLA" if (state_code == 9) & (district_code ==9)
	replace district_name = "SOLAN" if (state_code == 9) & (district_code ==10)
	replace district_name = "SIRMAUR" if (state_code == 9) & (district_code ==11)
	replace district_name = "KINAUR" if (state_code == 9) & (district_code ==12)
	replace district_name = "JAMMU AND KASHMIR" if (state_code == 10)
	replace district_name = "KARNATAKA" if (state_code == 11)
	replace district_name = "BANGALORE" if (state_code == 11) & (district_code ==1)
	replace district_name = "BANGALORE (RURAL)" if (state_code == 11) & (district_code ==2)
	replace district_name = "BELGAUM" if (state_code == 11) & (district_code ==3)
	replace district_name = "BELLARY" if (state_code == 11) & (district_code ==4)
	replace district_name = "BIDAR" if (state_code == 11) & (district_code ==5)
	replace district_name = "BIJAPUR" if (state_code == 11) & (district_code ==6)
	replace district_name = "CHIKMANGALUR" if (state_code == 11) & (district_code ==7)
	replace district_name = "CHITRADURGA" if (state_code == 11) & (district_code ==8)
	replace district_name = "DUKSHIN KANNAD" if (state_code == 11) & (district_code ==9)
	replace district_name = "DHARWAD" if (state_code == 11) & (district_code ==10)
	replace district_name = "GULBARGA" if (state_code == 11) & (district_code ==11)
	replace district_name = "HASSAN" if (state_code == 11) & (district_code ==12)
	replace district_name = "KADAGU" if (state_code == 11) & (district_code ==13)
	replace district_name = "KOLAR" if (state_code == 11) & (district_code ==14)
	replace district_name = "MANDYA" if (state_code == 11) & (district_code ==15)
	replace district_name = "MYSORE" if (state_code == 11) & (district_code ==16)
	replace district_name = "RIACHUR" if (state_code == 11) & (district_code ==17)
	replace district_name = "SHIMOGA" if (state_code == 11) & (district_code ==18)
	replace district_name = "TUMKUR" if (state_code == 11) & (district_code ==19)
	replace district_name = "UTTAR KANNAD" if (state_code == 11) & (district_code ==20)
	replace district_name = "KERALA" if (state_code == 12)
	replace district_name = "KOSARAGOD" if (state_code == 12) & (district_code ==1)
	replace district_name = "CANNANUR" if (state_code == 12) & (district_code ==2)
	replace district_name = "WAYANAD" if (state_code == 12) & (district_code ==3)
	replace district_name = "KAZHIKODE" if (state_code == 12) & (district_code ==4)
	replace district_name = "MALAPPURAM" if (state_code == 12) & (district_code ==5)
	replace district_name = "PALGHAT" if (state_code == 12) & (district_code ==6)
	replace district_name = "TRICHUR" if (state_code == 12) & (district_code ==7)
	replace district_name = "ERNAKULAM" if (state_code == 12) & (district_code ==8)
	replace district_name = "IDUKKI" if (state_code == 12) & (district_code ==9)
	replace district_name = "KOTTAYAM" if (state_code == 12) & (district_code ==10)
	replace district_name = "ALLEPPEY" if (state_code == 12) & (district_code ==11)
	replace district_name = "PATHANAMTHITTA" if (state_code == 12) & (district_code ==12)
	replace district_name = "QUILON" if (state_code == 12) & (district_code ==13)
	replace district_name = "TRIVANDRUM" if (state_code == 12) & (district_code ==14)
	replace district_name = "MADHYA PRADESH" if (state_code == 13)
	replace district_name = "MORENA" if (state_code == 13) & (district_code ==1)
	replace district_name = "BHIND" if (state_code == 13) & (district_code ==2)
	replace district_name = "GWALIOR" if (state_code == 13) & (district_code ==3)
	replace district_name = "DATIA" if (state_code == 13) & (district_code ==4)
	replace district_name = "SHIVPURI" if (state_code == 13) & (district_code ==5)
	replace district_name = "GUNA" if (state_code == 13) & (district_code ==6)
	replace district_name = "TIKAMAGARH" if (state_code == 13) & (district_code ==7)
	replace district_name = "CHHATARPUR" if (state_code == 13) & (district_code ==8)
	replace district_name = "PANNA" if (state_code == 13) & (district_code ==9)
	replace district_name = "SAGAR" if (state_code == 13) & (district_code ==10)
	replace district_name = "DOMOH" if (state_code == 13) & (district_code ==11)
	replace district_name = "SATNA" if (state_code == 13) & (district_code ==12)
	replace district_name = "REWA" if (state_code == 13) & (district_code ==13)
	replace district_name = "SHAHDOL" if (state_code == 13) & (district_code ==14)
	replace district_name = "SIDHI" if (state_code == 13) & (district_code ==15)
	replace district_name = "MANDSAUR" if (state_code == 13) & (district_code ==16)
	replace district_name = "RATLAM" if (state_code == 13) & (district_code ==17)
	replace district_name = "UJJAIN" if (state_code == 13) & (district_code ==18)
	replace district_name = "SHAJAPUR" if (state_code == 13) & (district_code ==19)
	replace district_name = "DEWAS" if (state_code == 13) & (district_code ==20)
	replace district_name = "JHABUA" if (state_code == 13) & (district_code ==21)
	replace district_name = "DHAR" if (state_code == 13) & (district_code ==22)
	replace district_name = "INDORE" if (state_code == 13) & (district_code ==23)
	replace district_name = "KHAARGONE" if (state_code == 13) & (district_code ==24)
	replace district_name = "KHANDWA" if (state_code == 13) & (district_code ==25)
	replace district_name = "RAJGARH" if (state_code == 13) & (district_code ==26)
	replace district_name = "VIDISHA" if (state_code == 13) & (district_code ==27)
	replace district_name = "BHOPAL" if (state_code == 13) & (district_code ==28)
	replace district_name = "SEHORE" if (state_code == 13) & (district_code ==29)
	replace district_name = "RAISEN" if (state_code == 13) & (district_code ==30)
	replace district_name = "BETUL" if (state_code == 13) & (district_code ==31)
	replace district_name = "HOSHANGABD" if (state_code == 13) & (district_code ==32)
	replace district_name = "JABALPUR" if (state_code == 13) & (district_code ==33)
	replace district_name = "NARSIMHAPUR" if (state_code == 13) & (district_code ==34)
	replace district_name = "MANDLA" if (state_code == 13) & (district_code ==35)
	replace district_name = "CHHINDWARA" if (state_code == 13) & (district_code ==36)
	replace district_name = "SEONI" if (state_code == 13) & (district_code ==37)
	replace district_name = "BALAGHAT" if (state_code == 13) & (district_code ==38)
	replace district_name = "SURGUJA" if (state_code == 13) & (district_code ==39)
	replace district_name = "BILASPUR" if (state_code == 13) & (district_code ==40)
	replace district_name = "RAIGARH" if (state_code == 13) & (district_code ==41)
	replace district_name = "RAIGARH RAJNANDGAON" if (state_code == 13) & (district_code ==42)
	replace district_name = "DURG" if (state_code == 13) & (district_code ==43)
	replace district_name = "RAIPUR" if (state_code == 13) & (district_code ==44)
	replace district_name = "BASTAR" if (state_code == 13) & (district_code ==45)
	replace district_name = "MAHARASTRA" if (state_code == 14)
	replace district_name = "GREATER BOMBAY" if (state_code == 14) & (district_code ==1)
	replace district_name = "THANE" if (state_code == 14) & (district_code ==2)
	replace district_name = "RAIGARH" if (state_code == 14) & (district_code ==3)
	replace district_name = "RATNAGIRI" if (state_code == 14) & (district_code ==4)
	replace district_name = "SINDHUDURG" if (state_code == 14) & (district_code ==5)
	replace district_name = "NASHIK" if (state_code == 14) & (district_code ==6)
	replace district_name = "DHULE" if (state_code == 14) & (district_code ==7)
	replace district_name = "JALGAON" if (state_code == 14) & (district_code ==8)
	replace district_name = "AHMADNAGAR" if (state_code == 14) & (district_code ==9)
	replace district_name = "PUNE" if (state_code == 14) & (district_code ==10)
	replace district_name = "SATARA" if (state_code == 14) & (district_code ==11)
	replace district_name = "SANGLI" if (state_code == 14) & (district_code ==12)
	replace district_name = "SOLAPUR" if (state_code == 14) & (district_code ==13)
	replace district_name = "KOLHAPUR" if (state_code == 14) & (district_code ==14)
	replace district_name = "AURANGABAD" if (state_code == 14) & (district_code ==15)
	replace district_name = "JALNA" if (state_code == 14) & (district_code ==16)
	replace district_name = "PARBHANI" if (state_code == 14) & (district_code ==17)
	replace district_name = "BID" if (state_code == 14) & (district_code ==18)
	replace district_name = "NANDED" if (state_code == 14) & (district_code ==19)
	replace district_name = "OSMANABAD" if (state_code == 14) & (district_code ==20)
	replace district_name = "LATUR" if (state_code == 14) & (district_code ==21)
	replace district_name = "BULDANA" if (state_code == 14) & (district_code ==22)
	replace district_name = "AKOLA" if (state_code == 14) & (district_code ==23)
	replace district_name = "AMRAVATI" if (state_code == 14) & (district_code ==24)
	replace district_name = "YAVATMAL" if (state_code == 14) & (district_code ==25)
	replace district_name = "WARDHA" if (state_code == 14) & (district_code ==26)
	replace district_name = "NAGPUR" if (state_code == 14) & (district_code ==27)
	replace district_name = "BHANDARA" if (state_code == 14) & (district_code ==28)
	replace district_name = "CHANDRAPUR" if (state_code == 14) & (district_code ==29)
	replace district_name = "GADCHIROLI" if (state_code == 14) & (district_code ==30)
	replace district_name = "MANIPUR" if (state_code == 15)
	replace district_name = "SENAPATI" if (state_code == 15) & (district_code ==1)
	replace district_name = "TAMENGLONG" if (state_code == 15) & (district_code ==2)
	replace district_name = "CHURACHANDPUR" if (state_code == 15) & (district_code ==3)
	replace district_name = "CHANDEL" if (state_code == 15) & (district_code ==4)
	replace district_name = "THOUBAL" if (state_code == 15) & (district_code ==5)
	replace district_name = "BISHNUPUR" if (state_code == 15) & (district_code ==6)
	replace district_name = "IMPHAL" if (state_code == 15) & (district_code ==7)
	replace district_name = "UKHRUL" if (state_code == 15) & (district_code ==8)
	replace district_name = "JAINTIA HILLS" if (state_code == 16) & (district_code ==1)
	replace district_name = "EAST KHASI HILLS" if (state_code == 16) & (district_code ==2)
	replace district_name = "WEST KHASI HILLS" if (state_code == 16) & (district_code ==3)
	replace district_name = "EAST GARO HILLS" if (state_code == 16) & (district_code ==4)
	replace district_name = "WEST GARO HILLS" if (state_code == 16) & (district_code ==5)
	replace district_name = "MIZORAM" if (state_code == 17)
	replace district_name = "AIZAWAL" if (state_code == 17) & (district_code ==1)
	replace district_name = "LUNGLEI" if (state_code == 17) & (district_code ==2)
	replace district_name = "CHHIMTUIPUI" if (state_code == 17) & (district_code ==3)
	replace district_name = "NAGALAND" if (state_code == 18)
	replace district_name = "KOHIMA" if (state_code == 18) & (district_code ==1)
	replace district_name = "PHEK" if (state_code == 18) & (district_code ==2)
	replace district_name = "ZUNHEBOTO" if (state_code == 18) & (district_code ==3)
	replace district_name = "WOKHA" if (state_code == 18) & (district_code ==4)
	replace district_name = "MOKOKCHUNG" if (state_code == 18) & (district_code ==5)
	replace district_name = "TUENSANG" if (state_code == 18) & (district_code ==6)
	replace district_name = "MON" if (state_code == 18) & (district_code ==7)
	replace district_name = "ORISSA" if (state_code == 19)
	replace district_name = "SAMBALPUR" if (state_code == 19) & (district_code ==1)
	replace district_name = "SUNDARGARH" if (state_code == 19) & (district_code ==2)
	replace district_name = "KENDUJHAR" if (state_code == 19) & (district_code ==3)
	replace district_name = "MAYURBHANJ" if (state_code == 19) & (district_code ==4)
	replace district_name = "BALESHWAR" if (state_code == 19) & (district_code ==5)
	replace district_name = "CUTTACK" if (state_code == 19) & (district_code ==6)
	replace district_name = "DHENKANAL" if (state_code == 19) & (district_code ==7)
	replace district_name = "PHULABANI" if (state_code == 19) & (district_code ==8)
	replace district_name = "BALANGIR" if (state_code == 19) & (district_code ==9)
	replace district_name = "KALAHANDI" if (state_code == 19) & (district_code ==10)
	replace district_name = "KORAPUT" if (state_code == 19) & (district_code ==11)
	replace district_name = "GANJAM" if (state_code == 19) & (district_code ==12)
	replace district_name = "PURI" if (state_code == 19) & (district_code ==13)
	replace district_name = "PUNJAB" if (state_code == 20)
	replace district_name = "GURDASPUR" if (state_code == 20) & (district_code ==1)
	replace district_name = "AMRITSAR" if (state_code == 20) & (district_code ==2)
	replace district_name = "FIROZPUR" if (state_code == 20) & (district_code ==3)
	replace district_name = "LUDHIANA" if (state_code == 20) & (district_code ==4)
	replace district_name = "JALANDHAR" if (state_code == 20) & (district_code ==5)
	replace district_name = "KAPURTHALA" if (state_code == 20) & (district_code ==6)
	replace district_name = "HOSHIAPUR" if (state_code == 20) & (district_code ==7)
	replace district_name = "RUPNAGAR" if (state_code == 20) & (district_code ==8)
	replace district_name = "PATIALA" if (state_code == 20) & (district_code ==9)
	replace district_name = "SANGRUR" if (state_code == 20) & (district_code ==10)
	replace district_name = "BATHINDA" if (state_code == 20) & (district_code ==11)
	replace district_name = "FARIDKOT" if (state_code == 20) & (district_code ==12)
	replace district_name = "RAJASTHAN" if (state_code == 21)
	replace district_name = "GANGANAGAR" if (state_code == 21) & (district_code ==1)
	replace district_name = "BIKANER" if (state_code == 21) & (district_code ==2)
	replace district_name = "CHURU" if (state_code == 21) & (district_code ==3)
	replace district_name = "JHUNJHUNUN" if (state_code == 21) & (district_code ==4)
	replace district_name = "ALWAR" if (state_code == 21) & (district_code ==5)
	replace district_name = "BHARATPUR" if (state_code == 21) & (district_code ==6)
	replace district_name = "DHOLPUR" if (state_code == 21) & (district_code ==7)
	replace district_name = "SWAIMADHOPUR" if (state_code == 21) & (district_code ==8)
	replace district_name = "JAIPUR" if (state_code == 21) & (district_code ==9)
	replace district_name = "SIKAR" if (state_code == 21) & (district_code ==10)
	replace district_name = "AJMER" if (state_code == 21) & (district_code ==11)
	replace district_name = "TONK" if (state_code == 21) & (district_code ==12)
	replace district_name = "JAISALMER" if (state_code == 21) & (district_code ==13)
	replace district_name = "JODHPUR" if (state_code == 21) & (district_code ==14)
	replace district_name = "NAGAUR" if (state_code == 21) & (district_code ==15)
	replace district_name = "PALI" if (state_code == 21) & (district_code ==16)
	replace district_name = "BARMER" if (state_code == 21) & (district_code ==17)
	replace district_name = "JALOR" if (state_code == 21) & (district_code ==18)
	replace district_name = "SIROHI" if (state_code == 21) & (district_code ==19)
	replace district_name = "BHILWARA" if (state_code == 21) & (district_code ==20)
	replace district_name = "UDAIPUR" if (state_code == 21) & (district_code ==21)
	replace district_name = "CHITTOURGARH" if (state_code == 21) & (district_code ==22)
	replace district_name = "DUNGARPUR" if (state_code == 21) & (district_code ==23)
	replace district_name = "BANSWARA" if (state_code == 21) & (district_code ==24)
	replace district_name = "BUNDI" if (state_code == 21) & (district_code ==25)
	replace district_name = "KOTA" if (state_code == 21) & (district_code ==26)
	replace district_name = "JHALAWAR" if (state_code == 21) & (district_code ==27)
	replace district_name = "SIKKIM" if (state_code == 22)
	replace district_name = "NORTH DISTRICT" if (state_code == 22) & (district_code ==1)
	replace district_name = "EAST DISTRICT" if (state_code == 22) & (district_code ==2)
	replace district_name = "SOUTH DISTRICIT" if (state_code == 22) & (district_code ==3)
	replace district_name = "WEST DISTRICT" if (state_code == 22) & (district_code ==4)
	replace district_name = "TAMILNADU" if (state_code == 23)
	replace district_name = "MADRAS" if (state_code == 23) & (district_code ==1)
	replace district_name = "CHENGAIANNA" if (state_code == 23) & (district_code ==2)
	replace district_name = "NORTH ARCOT AMBEDKAR" if (state_code == 23) & (district_code ==3)
	replace district_name = "DHARMAPURI" if (state_code == 23) & (district_code ==4)
	replace district_name = "TIRUVANMALAI SAMBUVR" if (state_code == 23) & (district_code ==5)
	replace district_name = "SOUTH ARCOT" if (state_code == 23) & (district_code ==6)
	replace district_name = "SALEM" if (state_code == 23) & (district_code ==7)
	replace district_name = "PERIYAR" if (state_code == 23) & (district_code ==8)
	replace district_name = "NILGIRI" if (state_code == 23) & (district_code ==9)
	replace district_name = "COIMBATORE" if (state_code == 23) & (district_code ==10)
	replace district_name = "DINDIGUL QUAIDE MILAT" if (state_code == 23) & (district_code ==11)
	replace district_name = "TIRUCHIRPALLI" if (state_code == 23) & (district_code ==12)
	replace district_name = "TANJURE" if (state_code == 23) & (district_code ==13)
	replace district_name = "PUDUKKOTTAI" if (state_code == 23) & (district_code ==14)
	replace district_name = "PASUMPON THEVAR" if (state_code == 23) & (district_code ==15)
	replace district_name = "MADURAI" if (state_code == 23) & (district_code ==16)
	replace district_name = "KAMARAJAN" if (state_code == 23) & (district_code ==17)
	replace district_name = "RAMANATHAPURAM" if (state_code == 23) & (district_code ==18)
	replace district_name = "CHIDAMBARAMAN" if (state_code == 23) & (district_code ==19)
	replace district_name = "TIRUNELVELI KATABOMN" if (state_code == 23) & (district_code ==20)
	replace district_name = "KANYAKUMARI" if (state_code == 23) & (district_code ==21)
	replace district_name = "TRIPURA" if (state_code == 24)
	replace district_name = "WEST TRIPURA" if (state_code == 24) & (district_code ==1)
	replace district_name = "NORTH TRIPURA" if (state_code == 24) & (district_code ==2)
	replace district_name = "SOUTH TRIPURA" if (state_code == 24) & (district_code ==3)
	replace district_name = "UTTAR PRADESH" if (state_code == 25)
	replace district_name = "UTTAR KASHI" if (state_code == 25) & (district_code ==1)
	replace district_name = "CHAMOLI" if (state_code == 25) & (district_code ==2)
	replace district_name = "TEHRI GARWAL" if (state_code == 25) & (district_code ==3)
	replace district_name = "DEHRADUN" if (state_code == 25) & (district_code ==4)
	replace district_name = "GARHWAL" if (state_code == 25) & (district_code ==5)
	replace district_name = "PITHORAGARH" if (state_code == 25) & (district_code ==6)
	replace district_name = "ALMORA" if (state_code == 25) & (district_code ==7)
	replace district_name = "NAINITAL" if (state_code == 25) & (district_code ==8)
	replace district_name = "BIJNOR" if (state_code == 25) & (district_code ==9)
	replace district_name = "MORADABAD" if (state_code == 25) & (district_code ==10)
	replace district_name = "RAMPUR" if (state_code == 25) & (district_code ==11)
	replace district_name = "BAHARANPUR" if (state_code == 25) & (district_code ==12)
	replace district_name = "HARDWAR" if (state_code == 25) & (district_code ==13)
	replace district_name = "MUZAFFAR NAGAR" if (state_code == 25) & (district_code ==14)
	replace district_name = "MEERUT" if (state_code == 25) & (district_code ==15)
	replace district_name = "GHAZIABAD" if (state_code == 25) & (district_code ==16)
	replace district_name = "BULANDSHAHR" if (state_code == 25) & (district_code ==17)
	replace district_name = "ALIGARH" if (state_code == 25) & (district_code ==18)
	replace district_name = "MATHURA" if (state_code == 25) & (district_code ==19)
	replace district_name = "AGRA" if (state_code == 25) & (district_code ==20)
	replace district_name = "FIROZABAD" if (state_code == 25) & (district_code ==21)
	replace district_name = "ETAH" if (state_code == 25) & (district_code ==22)
	replace district_name = "MAINPURI" if (state_code == 25) & (district_code ==23)
	replace district_name = "BUDAUN" if (state_code == 25) & (district_code ==24)
	replace district_name = "BAREILLY" if (state_code == 25) & (district_code ==25)
	replace district_name = "PILIBHIT" if (state_code == 25) & (district_code ==26)
	replace district_name = "SHAHJAHANPUR" if (state_code == 25) & (district_code ==27)
	replace district_name = "KHERI" if (state_code == 25) & (district_code ==28)
	replace district_name = "SITAPUR" if (state_code == 25) & (district_code ==29)
	replace district_name = "HARDOI" if (state_code == 25) & (district_code ==30)
	replace district_name = "UNNAO" if (state_code == 25) & (district_code ==31)
	replace district_name = "LUCKNOW" if (state_code == 25) & (district_code ==32)
	replace district_name = "RAIBARELI" if (state_code == 25) & (district_code ==33)
	replace district_name = "FARRUKHABAD" if (state_code == 25) & (district_code ==34)
	replace district_name = "ETAWAH" if (state_code == 25) & (district_code ==35)
	replace district_name = "KANPUR DEHAT" if (state_code == 25) & (district_code ==36)
	replace district_name = "KANPUR NAGAR" if (state_code == 25) & (district_code ==37)
	replace district_name = "JALAUN" if (state_code == 25) & (district_code ==38)
	replace district_name = "JHANSI" if (state_code == 25) & (district_code ==39)
	replace district_name = "LALITPUR" if (state_code == 25) & (district_code ==40)
	replace district_name = "HAMIRPUR" if (state_code == 25) & (district_code ==41)
	replace district_name = "BANDA" if (state_code == 25) & (district_code ==42)
	replace district_name = "FATEHPUR" if (state_code == 25) & (district_code ==43)
	replace district_name = "PRATAPGARH" if (state_code == 25) & (district_code ==44)
	replace district_name = "ALLAHABAD" if (state_code == 25) & (district_code ==45)
	replace district_name = "BAHRAICH" if (state_code == 25) & (district_code ==46)
	replace district_name = "GONDA" if (state_code == 25) & (district_code ==47)
	replace district_name = "BARABANKI" if (state_code == 25) & (district_code ==48)
	replace district_name = "FAIZABAD" if (state_code == 25) & (district_code ==49)
	replace district_name = "SULTANPUR" if (state_code == 25) & (district_code ==50)
	replace district_name = "SIDDARTH NAGAR" if (state_code == 25) & (district_code ==51)
	replace district_name = "MAHARAJGANJ" if (state_code == 25) & (district_code ==52)
	replace district_name = "BASTI" if (state_code == 25) & (district_code ==53)
	replace district_name = "GORAKHPUR" if (state_code == 25) & (district_code ==54)
	replace district_name = "DEORIA" if (state_code == 25) & (district_code ==55)
	replace district_name = "MAU" if (state_code == 25) & (district_code ==56)
	replace district_name = "AZAMGARH" if (state_code == 25) & (district_code ==57)
	replace district_name = "JAUNPU" if (state_code == 25) & (district_code ==58)
	replace district_name = "BALLIA" if (state_code == 25) & (district_code ==59)
	replace district_name = "GHAZIPUR" if (state_code == 25) & (district_code ==60)
	replace district_name = "VARANASI" if (state_code == 25) & (district_code ==61)
	replace district_name = "MIRZAPUR" if (state_code == 25) & (district_code ==62)
	replace district_name = "SONBHADRA" if (state_code == 25) & (district_code ==63)
	replace district_name = "WEST BENGAL" if (state_code == 26)
	replace district_name = "KOCH BIHAR" if (state_code == 26) & (district_code ==1)
	replace district_name = "JALPAIGURI" if (state_code == 26) & (district_code ==2)
	replace district_name = "DARJLING" if (state_code == 26) & (district_code ==3)
	replace district_name = "WEST DINAJPUR" if (state_code == 26) & (district_code ==4)
	replace district_name = "MALDAH" if (state_code == 26) & (district_code ==5)
	replace district_name = "MURSHIDABAD" if (state_code == 26) & (district_code ==6)
	replace district_name = "NADIA" if (state_code == 26) & (district_code ==7)
	replace district_name = "NORTH 24 PARGANAS" if (state_code == 26) & (district_code ==8)
	replace district_name = "SOUTH 24 PARGANAS" if (state_code == 26) & (district_code ==9)
	replace district_name = "CALCUTTA" if (state_code == 26) & (district_code ==10)
	replace district_name = "HAORA" if (state_code == 26) & (district_code ==11)
	replace district_name = "HUGLI" if (state_code == 26) & (district_code ==12)
	replace district_name = "MEDINIPUR" if (state_code == 26) & (district_code ==13)
	replace district_name = "BANKURA" if (state_code == 26) & (district_code ==14)
	replace district_name = "PURULIYA" if (state_code == 26) & (district_code ==15)
	replace district_name = "BARDDHAMAN" if (state_code == 26) & (district_code ==16)
	replace district_name = "BIRBHUM" if (state_code == 26) & (district_code ==17)
	replace district_name = "ANDAMANS AND NOCOBAR" if (state_code == 27)
	replace district_name = "ANDAMANS" if (state_code == 27) & (district_code ==1)
	replace district_name = "NICOBARS" if (state_code == 27) & (district_code ==2)
	replace district_name = "CHANDIGARH" if (state_code == 28)
	replace district_name = "CHANDIGARH" if (state_code == 28) & (district_code ==1)
	replace district_name = "DADRA & NAGAR HAVELI" if (state_code == 29)
	replace district_name = "DADRA & NAGAR HAVELI" if (state_code == 29) & (district_code ==1)
	replace district_name = "DAMAN AND DIU" if (state_code == 30)
	replace district_name = "DAMAN" if (state_code == 30) & (district_code ==1)
	replace district_name = "DIU" if (state_code == 30) & (district_code ==2)
	replace district_name = "DELHI" if (state_code == 31)
	replace district_name = "DELHI DISTRICT" if (state_code == 31) & (district_code ==1)
	replace district_name = "LAKSHSWEEP" if (state_code == 32)
	replace district_name = "LASKHDWEEP DISTT" if (state_code == 32) & (district_code ==1)
	replace district_name = "PONDICHERRY" if (state_code == 33)
	replace district_name = "PONDICHERRY" if (state_code == 33) & (district_code ==1)
	replace district_name = "KARAIKAL" if (state_code == 33) & (district_code ==2)
	replace district_name = "MAHE" if (state_code == 33) & (district_code ==3)
	replace district_name = "YANAM" if (state_code == 33) & (district_code ==4)
	
	compress * 
	save "$ec/1990/stata/ec_1990_st_`i'.dta", replace

}
}

********************************************************************************
********************************************************************************

** Step 2: READ IN 1998 DATA
{

//***read in delimited data EC4DT2C.txt - EC4DT36C.txt***//
//save in the same folder as EC4DT2C.dta - EC4DT36C.dta//

cd "$ec/1998/raw"

forval i = 2/36 {
	infile using "$ec/1998/dictionaries/dictionary`i'c.dct", clear

	* change all strings to upper case and trim white space
	ds, has(type string) 
	foreach v in `r(varlist)' { 
		replace `v' = upper(`v') 
	} 

	//recode state code into a new descriptive variable state (state name)//
	generate strL state = "."
	format %25s state

	replace state = "ANDHRA PRADESH" if (state_code == 2)
	replace state = "ARUNACHAL PRADESH" if (state_code == 3)
	replace state = "ASSAM" if (state_code == 4)
	replace state = "BIHAR" if (state_code == 5)
	replace state = "GOA" if (state_code == 6)
	replace state = "GUJARAT" if (state_code == 7)
	replace state = "HARYANA" if (state_code == 8)
	replace state = "HIMACHAL PRADESH" if (state_code == 9)
	replace state = "JAMMU & KASHMIR" if (state_code == 10)
	replace state = "KARNATAKA" if (state_code == 11)
	replace state = "KERALA" if (state_code == 12)
	replace state = "MADHYA PRADESH" if (state_code == 13)
	replace state = "MAHARASHTRA" if (state_code == 14)
	replace state = "MANIPUR" if (state_code == 15)
	replace state = "MEGHALAYA" if (state_code == 16)
	replace state = "MIZORAM" if (state_code == 17)
	replace state = "NAGALAND" if (state_code == 18)
	replace state = "ORISSA" if (state_code == 19)
	replace state = "PUNJAB" if (state_code == 20)
	replace state = "RAJASTHAN" if (state_code == 21)
	replace state = "SIKKIM" if (state_code == 22)
	replace state = "TAMIL NADU" if (state_code == 23)
	replace state = "TRIPURA" if (state_code == 24)
	replace state = "UTTAR PRADESH" if (state_code == 25)
	replace state = "WEST BENGAL" if (state_code == 26)
	replace state = "ANDAMAN & NICOBAR ISLANDS" if (state_code == 27)
	replace state = "HANDIGARH" if (state_code == 28)
	replace state = "DADARA & NAGARHAVELI" if (state_code == 29)
	replace state = "DAMAN & DIU" if (state_code == 30)
	replace state = "DELHI" if (state_code == 31)
	replace state = "LAKSHDWEEP" if (state_code == 32)
	replace state = "PONDICHERRY" if (state_code == 33)
	replace state = "JHARKHAND" if (state_code == 34)
	replace state = "CHHATTISGARH" if (state_code == 35)
	replace state = "UTTARANCHAL" if (state_code == 36)

	//recode major activity code into a new variable activity//
	generate strL broadactivity = "."
	format %60s broadactivity
	replace broadactivity = "RAISING OF LIVE STOCK" if (broadactivity_code == 1)
	replace broadactivity = "AGRICULTURAL SERVICES(HUNTING, FORESTRY & FISHING)" if (broadactivity_code == 2)
	replace broadactivity = "ALL AGRICULRURAL ACTIVITIES(SR NO 1 & 2)" if (broadactivity_code == 3)
	replace broadactivity = "MINING & QUARRYING" if (broadactivity_code == 4)
	replace broadactivity = "MANUFACTURING" if (broadactivity_code == 5)
	replace broadactivity = "ELECTRICITY, GAS & WATER" if (broadactivity_code == 6)
	replace broadactivity = "CONSTRUCTION" if (broadactivity_code == 7)
	replace broadactivity = "WHOLESALE TRADE" if (broadactivity_code == 8)
	replace broadactivity = "RETAIL TRADE" if (broadactivity_code == 9)
	replace broadactivity = "RESTAURANTS & HOTELS" if (broadactivity_code == 10)
	replace broadactivity = "TRANSPORT" if (broadactivity_code == 11)
	replace broadactivity = "STORAGE & WAREHOUSING" if (broadactivity_code == 12)
	replace broadactivity = "COMMUNICATIONS" if (broadactivity_code == 13) 
	replace broadactivity = "FINANCIAL,INSURANCE,REAL ESTATE AND BUSIINESS SERVICES" if (broadactivity_code == 14)
	replace broadactivity = "COMMUNITY, SOCIAL AND PERSONAL SERVICES" if (broadactivity_code == 15)
	replace broadactivity = "OTHER(UNSPECIFIED) ACTIVITIES" if (broadactivity_code == 16)
	replace broadactivity = "ALL NON-AGRICULTURAL ACTIVITIES (SR NO 4 TO 16)" if (broadactivity_code == 17)
	replace broadactivity = "ALL NON-AGRICULTURAL AND NON-AGRICULTURAL ACTIVITIES" if (broadactivity_code == 18)

	//recode status of premises into dummy//
	generate float premises_dummy = 0
	replace premises_dummy = 1 if premises == 2
	replace premises_dummy =0 if premises == 1
	drop premises

	//recode operation nature and generate a new dummy variable perennial//
	generate float perennial_dummy = 0
	replace perennial_dummy = 1 if operationnature_code == 1

	//recode ownership code into ownership//
	generate str ownership = "."
	format %35s ownership
	replace ownership = "Private Non-Profit Institutions" if ownership_code == 1
	replace ownership = "Private Others" if ownership_code == 2
	replace ownership = "Co-operatives" if ownership_code == 3
	replace ownership = "Governments" if ownership_code == 4

	//recode power fuel use from code to descriptions//
	generate strL powerfuel = "."
	format %60s powerfuel
	replace powerfuel = "without power" if (powerfuel_code == 0) & (powerfuel_code !=.)
	replace powerfuel = "Electricity" if powerfuel_code == 1
	replace powerfuel = "Coal/soft coke" if powerfuel_code == 2
	replace powerfuel = "Patrol/Diesel " if powerfuel_code == 3
	replace powerfuel = "LPG/Natural Gas" if powerfuel_code == 4
	replace powerfuel = "Fire wood" if powerfuel_code == 5
	replace powerfuel = "Kerosene" if powerfuel_code == 6
	replace powerfuel = "Animal power" if powerfuel_code == 7
	replace powerfuel = "Non-conventional energy(bio-gas, solar & wind energy)" if powerfuel_code == 8
	replace powerfuel = "others" if powerfuel_code == 9
	format %60s powerfuel

	//recode financesource from code to description//
	generate financesource = "."
	format %80s financesource
	replace financesource = "Assistance under IRDP" if financesource_code == 1
	replace financesource = "Assistance under other poverty alleviation Programmes TRYSEM/DWCRA/Tool Kits" if financesource_code == 2
	replace financesource = "Borrowing from Institutions" if financesource_code == 3
	replace financesource = " Borrowing from non-institutions" if financesource_code == 4
	replace financesource = "Self-financing" if financesource_code == 5
	replace financesource = "Others" if financesource_code == 6

	//enterprisetype recode from numeric values to a new descriptive variable//
	generate str directory = "."
	replace directory = "OAE" if directory_code  == 1
	replace directory = "NDE" if directory_code  == 2
	replace directory = "DE" if directory_code  == 3


	//socialgroupcode recoded to sical gorup of owner//
	generate socialgroup = "."
	replace socialgroup = "ST Female" if socialgroup_code  == 1
	replace socialgroup = "ST Male" if socialgroup_code  == 2
	replace socialgroup = "SC Female" if socialgroup_code  == 3
	replace socialgroup = "SC Male" if socialgroup_code  == 4
	replace socialgroup = "OBC Female" if socialgroup_code  == 5
	replace socialgroup = "OBC Male" if socialgroup_code  == 6
	replace socialgroup = "Other Female" if socialgroup_code  == 7
	replace socialgroup = "Other Male" if socialgroup_code  == 8
	replace socialgroup = "Other than private enterprises" if socialgroup_code  == 9


	//employmentsizecode1 recoded//
	tostring employmentsize1, replace
	tostring employmentsize2, replace
	recast strL employmentsize1 employmentsize2
	replace employmentsize1 = "0-1" if employmentsize1 == "1"
	replace employmentsize1 = "6-9" if employmentsize1 == "6"
	replace employmentsize1 = ">9" if employmentsize1 == "7"


	//employmentsizecode2 recoded//
	replace employmentsize2 = "0-1" if employmentsize2 == "1"
	replace employmentsize2 = "6-9" if employmentsize2 == "7"
	replace employmentsize2 = "10-14" if employmentsize2 == "8"
	replace employmentsize2 = "15-19" if employmentsize2 == "9"
	replace employmentsize2 = "20-24" if employmentsize2 == "10"
	replace employmentsize2 = "25-49" if employmentsize2 == "11"
	replace employmentsize2 = "50-99" if employmentsize2 == "12"
	replace employmentsize2 = "100-199" if employmentsize2 == "13"
	replace employmentsize2 = "200-499" if employmentsize2 == "14"
	replace employmentsize2 = ">=500" if employmentsize2 == "15"


	//labeling some variables//
	label variable operationnature_code "Perennial - 1 , Seasonal - 2"   
	label variable perennial "dummy variable, Perennial - 1, Non-Perennial -2"
	label variable premises_dummy "dummy variable, Premises - 1, Non-Premises -0 "
	label variable id "IDENTIFICATION NO (ID NO), for rural or urban"


	compress  *
	save "$ec/1998/stata/Temp/ec_1998_st_`i'.dta", replace
	clear
}


/*************************Read in Location files****************************/
forval i = 2/9 {

	/**first read in rural location dataset
	Rural dataset needs to be split up - first row is state
	Second row is district
	Third row is tehsil
	Remaining are villagetowns. Then repeat this pattern. 
	Steps below "reshapes" the data from long to wide by which location the road refers to
	**/
	if (`i' > 1 & `i' < 10) ///
		infix str12 id 1-12 str12 name 20-40 using "$ec/1998/raw/EC4DIR02_17/EC4DR0`i'R.NDP", clear 
	if (`i' > 11 & `i' < 18) ///
		infix str12 id 1-12 str12 name 20-40 using "$ec/1998/raw/EC4DIR02_17/EC4DR`i'R.NDP", clear
	if (`i' > 18) ///
		infix str12 id 1-12 str12 villagetown_name 20-40 using "$ec/1998/raw/EC4DIR18_36/EC4DR`i'R.NDP", clear
	
	keep if id != ""
	
	gen type = "villagetown_name"
	replace type = "cdblock_name" if substr(id, 10, 12) == "00"
	replace type = "tehsil_name" if substr(id, 8, 12) == "00000"
	replace type = "district_name" if substr(id, 5, 12) == "00000000"
	replace type = "state" if substr(id, 3, 12) == "0000000000"
	
	gen district_name = name if type == "district_name"
	replace district_name = district_name[_n - 1] if missing(district_name)
	
	gen tehsil_name = name if type == "tehsil_name"
	replace tehsil_name = tehsil_name[_n-1] if missing(tehsil_name)
	
	gen cdblock_name = name if type == "cdblock_name"
	replace cdblock_name = cdblock_name[_n - 1] if missing(cdblock_name)
	
	keep if type == "villagetown_name"
	drop type
	rename name villagetown_name
	
	merge 1:m id using "$ec/1998/stata/Temp/ec_1998_st_`i'.dta"

	drop if _merge == 1
	drop _merge

	save "$ec/1998/stata/Temp/ec_1998_st_`i'.dta", replace


	///join urban location dataset
	if (`i' > 1 & `i' < 10) ///
		infix str12 id 78-89 ///
		  str12 villagetown_name 40-56 ///
		  strL district_name 23-37 ///
		  using "$ec/1998/raw/EC4DIR02_17/EC4DR0`i'U.NDP", clear
	if (`i' > 11 & `i' < 18) ///
		infix str12 id 78-89 ///
		  str12 villagetown_name 40-56 ///
		  strL district_name 23-37 ///
		  using "$ec/1998/raw/EC4DIR02_17/EC4DR`i'U.NDP", clear
	if (`i' > 18) ///
		infix str12 id 78-89 ///
		  str12 villagetown_name 40-56 ///
		  strL district_name 23-37 ///
		  using "$dirpath_ec98_raw/EC4DIR18_36/EC4DR`i'U.NDP", clear
	
	merge 1:m id using "$ec/1998/stata/Temp/ec_1998_st_`i'.dta"
	
	drop if _merge == 1
	drop _merge

	save "$ec/1998/stata/ec_1998_st_`i'.dta", replace
}
}

********************************************************************************
********************************************************************************

** Step 3: READ IN 2005 DATA
{
//read in raw data ec05d1c.txt - ec05d35c.txt///
local dictionary_files : dir "$ec/2005/dictionaries" files "*.dct"
cd "$ec/2005/raw/ec05entp"

foreach file in `dictionary_files' {
	infile using "$ec/2005/dictionaries/`file'", clear

	* change all strings to upper case and trim white space
	ds, has(type string) 
	foreach v in `r(varlist)' { 
		replace `v' = upper(`v') 
	} 
	
	/******************separate rural and urban codes*****************************/
	/**separate layouts for urban and rural, such that characters 8-23 generate 2 
	different sets of variables. Dictionaries read in 8-23 as one long string **/
	gen tehsil_code = "."
	gen village_code = "."
	gen enumeration_code = "."
	gen town_code = "."
	gen frame_code = "."
	gen ward_code = "."
	gen ivno = "."
	gen ufsblock = "."

	//rural schedule
	replace tehsil_code = substr(temp_id, 1, 4) if sector == 1
	replace village_code = substr(temp_id, 5, 8) if sector == 1
	replace enumeration_code = substr(temp_id, 13, 4) if sector == 1

	//urban schedule
	replace town_code = substr(temp_id, 1, 2) if sector == 2
	replace frame_code = substr(temp_id, 3, 2) if sector == 2
	replace ward_code = substr(temp_id, 5, 5) if sector == 2
	replace ivno = substr(temp_id, 10, 5) if sector == 2
	replace ufsblock = substr(temp_id, 15, 2) if sector == 2

	//drop temporary id created to hold both the rural and urban schedule ids
	drop temp_id
	
	gen villagetown_code = town_code
	replace villagetown_code = village_code if sector == 1
	


	/***********************add variable labels***********************/
	//***label variables***//
	label data "This file combines 27 data sets from ec3dt01c-35c. No data sets from 09c, 19c, 24c, 27c, 28c, 29c, 32c, 33c"
	label variable sector "1-Rural, 2-Urban"
	label variable state "01-35"
	label variable district "district code 0-63"
	label variable villagetown_code "Village/Town code"
	label variable ward_code "Ward/Mohalla/Hamlet-code" 
	label variable enumeration_code "Enumeration Block No." 
	label variable enterprise "enterprise premises status code 1, 2"
	label variable running_serial "running serial no of the enterprise within a page"
	label variable activity_major "activity code (major or subsidiary) 1, 2"
	label variable nic_code "major activity nic - 2004 code"
	label variable agriculture_dummy "1 - agriculture, 0 - nonagriculture"
	label variable operationnature_code "1 - Perennial, 2 - seasonal"
	label variable total_of_total "sum of totaladult_male, totaladult_female, totalchild_male, totalchild_female"



	/***************generate values from code variables and recode***************/
	generate strL state = "."
	format %25s state
	replace state = "JAMMU & KASHMIR" if (state_code == 1)
	replace state = "HIMACHAL PRADESH" if (state_code == 2)
	replace state = "PUNJAB" if (state_code == 3)
	replace state = "CHANDIGARH" if (state_code == 4)
	replace state = "UTTARANCHAL" if (state_code == 5)
	replace state = "HARYANA" if (state_code == 6)
	replace state = "DELHI" if (state_code == 7)
	replace state = "RAJASTHAN" if (state_code == 8)
	replace state = "UTTAR PRADESH" if (state_code == 9)
	replace state = "BIHAR" if (state_code == 10)
	replace state = "SIKKIM" if (state_code == 11)
	replace state = "ARUNACHAL PRADESH" if (state_code == 12)
	replace state = "NAGALAND" if (state_code == 13)
	replace state = "MANIPUR" if (state_code == 14)
	replace state = "MIZORAM" if (state_code == 15)
	replace state = "TRIPURA" if (state_code == 16)
	replace state = "MEGHALAYA" if (state_code == 17)
	replace state = "ASSAM" if (state_code == 18)
	replace state = "WEST BENGAL" if (state_code == 19)
	replace state = "JHARKHAND" if (state_code == 20)
	replace state = "ORISSA" if (state_code == 21)
	replace state = "CHHATTISGARH" if (state_code == 22)
	replace state = "MADHYA PRADESH" if (state_code == 23)
	replace state = "GUJARAT" if (state_code == 24)
	replace state = "DAMAN AND DIU" if (state_code == 25)
	replace state = "DADRA & NAGAR HAVELI" if (state_code == 26)
	replace state = "MAHARASHTRA" if (state_code == 27)
	replace state = "ANDHRA PRADESH" if (state_code == 28)
	replace state = "KARNATAKA" if (state_code == 29)
	replace state = "GOA" if (state_code == 30)
	replace state = "LAKSHADWEEP" if (state_code == 31)
	replace state = "KERALA" if (state_code == 32)
	replace state = "TAMIL NADU" if (state_code == 33)
	replace state = "PONDICHERRY" if (state_code == 34)
	replace state = "ANDAMAN & NICOBAR ISLAND" if (state_code == 35)


	generate byte perennial_dummy = 0
	replace perennial_dummy = 1 if operationnature_code == 1

	generate ownership = "."
	replace ownership = "Govt & PSU" if ownership_code == 1
	replace ownership = "Non profit Insts" if ownership_code == 2
	replace ownership = "Unincorporated proprietary" if ownership_code == 3
	replace ownership = "Unincorporated partnership" if ownership_code == 4
	replace ownership = "Corporate non financial" if ownership_code == 5
	replace ownership = "Corporate  financial" if ownership_code == 6
	replace ownership = "Co-operative" if ownership_code == 7

	generate socialgroup = "."
	replace socialgroup = "Female ST" if socialgroup_code == 1
	replace socialgroup = "Female SC" if socialgroup_code == 2
	replace socialgroup = "Female OBC" if socialgroup_code == 3
	replace socialgroup = "Female Other" if socialgroup_code == 4
	replace socialgroup = "Male ST" if socialgroup_code == 5
	replace socialgroup = "Male SC" if socialgroup_code == 6
	replace socialgroup = "Male Other" if socialgroup_code == 7
	replace socialgroup = "Other Male" if socialgroup_code == 8
	replace socialgroup = "Other then private enterprise" if socialgroup_code == 9

	generate powerfuel= "."
	replace powerfuel = "Without Power" if powerfuel_code == 1
	replace powerfuel = "Electricity" if powerfuel_code == 2
	replace powerfuel = "Coal/soft coke" if powerfuel_code == 3
	replace powerfuel = "petrol/diesel" if powerfuel_code == 4
	replace powerfuel = "LPG/Natural Gas" if powerfuel_code == 5
	replace powerfuel = "Kerosene" if powerfuel_code == 6
	replace powerfuel = "Animal Power" if powerfuel_code == 7
	replace powerfuel = "Non-conventional Energy" if powerfuel_code == 8
	replace powerfuel = "Others" if powerfuel_code == 9


	generate registration = "."
	format %8s state
	replace registration = "Not Registered" if registrationcode1 == 0
	replace registration = "Registered Under: Factory Act 1948" if registrationcode1 == 1
	replace registration = "Registered Under: State Directorate of Industries" if registrationcode1 == 2
	replace registration = "Registered Under: KVIC/KVIV" if registrationcode1 == 3
	replace registration = "Registered Under: Powerloom/Handloom/handicraft" if registrationcode1 == 4
	replace registration = "Registered Under: Texttile Commissioner/Jute Commissioner/Coir Board/Central Silk Board" if registrationcode1 == 5
	replace registration = "Registered Under: Central Excise/Sales Tax Act" if registrationcode1 == 6
	replace registration = "Registered Under: Shop & Establishment Act" if registrationcode1 == 7
	replace registration = "Registered Under: Co-operative Society/Labour Act" if registrationcode1 == 8
	replace registration = "Registered Under: Registered with other agencies" if registrationcode1 == 9

	generate financesource = "."
	replace financesource = "No Finance/self finance" if financesource_code == 0
	replace financesource = "Assistance from Govt sources" if financesource_code == 1
	replace financesource = "Borrowing from financial Institn" if financesource_code == 2
	replace financesource = "Borrowing from Non Institn/Money Lenders" if financesource_code == 3
	replace financesource = "Others like NGO Voluntary Orgn" if financesource_code == 9


	replace agriculture_dummy = 1 if agriculture_dummy == 2

	local i = state_code
	compress *
	save "$ec/2005/stata/Temp/ec_2005_st_`i'.dta", replace


	/**************************merge in location variables*********************/

	//merge in rural locations
	use "$ec/2005/raw/location codes/`i'rural.dta", clear
	foreach var of varlist * {
	 rename `var' `=lower("`var'")'
	}

	keep statecode districtname districtcode subdistrictname subdistrictcode ///
		 villagename villagecode

	rename statecode state_code
	rename districtname district_name
	rename districtcode district_code
	rename subdistrictname tehsil_name
	rename subdistrictcode tehsil_code
	rename villagename village_name
	rename villagecode village_code
	destring district_code, replace

	// want to change state_code to a string to match ec data
	// first have to convert NAs to . 
	
	destring state_code, replace force
	replace state_code = `i' if state_code == .
	
	duplicates drop state_code district_code tehsil_code village_code, force
	merge 1:m state_code district_code tehsil_code village_code using "$ec/2005/stata/Temp/ec_2005_st_`i'.dta"
	drop if _merge == 1
	drop _merge

	save "$ec/2005/stata/Temp/ec_2005_st_`i'.dta", replace

	/*****************merge in urban locations*****************************/
	use "$ec/2005/raw/location codes/`i'urban.dta", clear
	foreach var of varlist * {
	 rename `var' `=lower("`var'")'
	}

	keep statecode districtcode districtname towncode townname
	rename statecode state_code
	rename districtname district_name
	rename districtcode district_code
	rename towncode town_code
	rename townname town_name
	
	capture confirm string variable state_code
	if !_rc{
			drop if state_code == "NA"
			destring state_code, replace force

			}
			else{
				di in red "state_code is all good"
			}
		
	replace state_code = `i' if state_code == .
			
	destring district_code, replace
	destring state_code, replace
	duplicates drop state_code district_code town_code, force

	merge 1:m state_code district_code town_code using "$ec/2005/stata/Temp/ec_2005_st_`i'.dta"
	drop if _merge == 1
	drop _merge
	
	gen villagetown_name = town_name
	replace villagetown_name = village_name if sector == 1

	save "$ec/2005/stata/ec_2005_st_`i'.dta", replace

}

** DELETE OLD TEMP FILES
clear
cd "$ec/2005/stata/Temp"
local files: dir . files "*.dta"
qui foreach f in `files' {
	erase "`f'"
}
}

********************************************************************************
********************************************************************************

** Step 4: READ IN 2013 DATA
{
//create dta of nic codes
import excel "$ec/2013/Docs/NIC2008_3_DIGIT_CODE_LIST.xls", sheet("BACT_NIC_table") firstrow case(lower) clear
rename nic20083digitcode nic_code
rename description nic
duplicates drop nic_code, force

label variable nic_code "National Industrial Classification, 3 characters"
destring nic_code, replace

save "$ec/2013/stata/ec_2013_activity_nic_crosswalk.dta", replace

/***********************label existing variables************************/
cd "$ec/2013/raw"

foreach i of numlist 1/27 29/37{
	infile using "$ec/2013/dictionaries/dictionary`i'.dct", clear
	
	* change all strings to upper case and trim white space
	ds, has(type string) 
	foreach v in `r(varlist)' { 
		replace `v' = upper(`v') 
	} 
	

	label variable hired_male "number of hired male"
	label variable hired_female "number of hired female"
	label variable nonhired_male "number of non-hired male"
	label variable nonhired_female "number of non-hired male"
	label variable total_of_total "number of total hired people"
	label variable structure_code "Use of Census House/structure Code: 1,2,3 & 9" 
	label variable handloom_or_handicraft "Dummy variable(Yes-1, No-0)"
	label variable ownership_code "Valid codes are 1 to 7 & 9"
	label variable religion_code "Religion of the owner: 1 to 7 & 9"
	label variable operationnature_code "Code of Nature of Operation: 1,2 & 9"
	label variable financesource_code "Valid codes are 1-5 & 9"
	label variable state_code "State code, 2 characters"
	label variable district_code "District code, 2 characters"

	generate use_ofcensus = "."
	label variable use_ofcensus "Use of Census House/structure "
	replace use_ofcensus = "Commercial" if structure_code == 1
	replace use_ofcensus = "Residential" if structure_code == 2
	replace use_ofcensus = "Residential cum commercial" if structure_code == 3
	replace use_ofcensus = "Others including vacant, demolished, inaccessible, Govt buildings, etc.(Houses/Buildings/Structures housing activities not covered under Sixth Economic Census)" if structure_code == 9
	order structure_code, before(use_ofcensus)


	generate strL broadactivity = ""
	format broadactivity %200s
	label variable broadactivity "All the economic activities except crop production & plantation, public administration, defence and compulsory social security have been divided into 23 broad activities."
	replace broadactivity = "Activities relating to agriculture other than crop production and plantation" if broadactivity_code == 1
	replace broadactivity = "Livestock" if broadactivity_code == 2
	replace broadactivity = "Forestry and logging" if broadactivity_code == 3
	replace broadactivity = "Fishing and aquaculture" if broadactivity_code == 4
	replace broadactivity = "Mining and quarrying" if broadactivity_code == 5
	replace broadactivity = "Manufacturing1(including repair & installation of machinery & equipment)" if broadactivity_code == 6
	replace broadactivity = "Electricity , gas, steam and air conditioning supply" if broadactivity_code == 7
	replace broadactivity = "Water supply; sewerage, waste management and remediation activities" if broadactivity_code == 8
	replace broadactivity = "Construction" if broadactivity_code == 9
	replace broadactivity = "Whole sale trade, retail trade of motor vehicles & repair of motor vehicles & motor cycles" if broadactivity_code == 10
	replace broadactivity = "Whole sale trade,( other than motor vehicles and motor cycles)" if broadactivity_code == 11
	replace broadactivity = "Retail trade (excluding motor vehicles & motor cycles)" if broadactivity_code == 12
	replace broadactivity = "Transportation and storage (including postal & courier service)" if broadactivity_code == 13
	replace broadactivity = "Accommodation and food service activities" if broadactivity_code == 14
	replace broadactivity = "Information & communication(publishing, motion picture, telecommunication)" if broadactivity_code == 15
	replace broadactivity = "Financial & insurance activities (except compulsory social security)" if broadactivity_code == 16
	replace broadactivity = "Real estate activities" if broadactivity_code == 17
	replace broadactivity = "Professional, scientific & technical activities (legal, accounting, architecture)" if broadactivity_code == 18
	replace broadactivity = "Administrative and support service activities (employment agencies, travel agency)" if broadactivity_code == 19
	replace broadactivity = "Education" if broadactivity_code == 20
	replace broadactivity = "Human health & social work activities" if broadactivity_code == 21
	replace broadactivity = "Arts, entertainment, sports & amusement and recreation" if broadactivity_code == 22
	replace broadactivity = "Other service activities not elsewhere classified (including membership organization, repair of computers and personal household goods excluding activities of household as employers of domestic personnel)" if broadactivity_code == 23
	replace broadactivity = "All activities outside the coverage of Sixth Economic Census" if broadactivity_code == 99
	order broadactivity_code, before(broadactivity)


	generate ownership = "."
	replace ownership = "Government / PSU owned by Centre/State/Local govt" if ownership_code == 1
	replace ownership = "Proprietary" if ownership_code == 2
	replace ownership = "Partnership" if ownership_code == 3
	replace ownership = "Company" if ownership_code == 4
	replace ownership = "Self Help Group" if ownership_code == 5
	replace ownership = "Cooperatives" if ownership_code == 6
	replace ownership = "Non Profit Institution" if ownership_code == 7
	replace ownership = "Others" if ownership_code == 9
	order ownership_code, before(ownership)

	generate sex = "."
	replace sex = "male" if sex_code == 1
	replace sex = "female" if sex_code == 1
	replace sex = "other" if sex_code == 9
	order sex_code, before(sex)

	generate socialgroup = ""
	replace socialgroup = "SC" if socialgroup_code == 1
	replace socialgroup = "ST" if socialgroup_code == 2
	replace socialgroup = "OBC" if socialgroup_code == 3
	replace socialgroup = "Others" if socialgroup_code == 9
	order socialgroup_code, before(socialgroup)

	generate religion = "." 
	replace religion = "Hindu" if religion_code == 1
	replace religion = "Islam" if religion_code == 2
	replace religion = "Christian" if religion_code == 3
	replace religion = "Sikh" if religion_code == 4
	replace religion = "Buddhist" if religion_code == 5
	replace religion = "Zoroastrian(Jews)" if religion_code == 6
	replace religion = "Jain" if religion_code == 7
	replace religion = "Others" if religion_code == 9
	order religion_code, before (religion)

	gen operationnature = "."
	replace operationnature = "perennial" if operationnature_code == 1
	replace operationnature = "seasonal" if operationnature_code == 2
	replace operationnature = "casual" if operationnature_code == 9
	order operationnature_code, before (operationnature)

	generate byte perennial_dummy = 0
	replace perennial_dummy = 1 if operationnature_code == 1

	generate financesource = "."
	replace financesource = "self finance" if financesource_code == 1
	replace financesource = "financial assistance from govn. sources" if financesource_code == 2
	replace financesource = "borrowing from financial institutions" if financesource_code == 3
	replace financesource = "borrowing from non-institutions or money lenders" if financesource_code == 4
	replace financesource = "loan from self-help group" if financesource_code == 5
	replace financesource = "donations/transfers from other agencies" if financesource_code == 9
	order financesource_code, before (financesource)


	compress *
	save "$ec/2013/stata/Temp/EC6A_ST`i'.dta", replace
}


** read in location files and merge in vt_code

clear

foreach i of numlist 1/9{
	import excel using "$ec/2013/raw/location codes/ST0`i'.xlsx", firstrow case(lower) clear
	destring state_code district_code tehsil_code vt_code ward_code, replace
	duplicates drop state_code district_code tehsil_code vt_code, force
	rename vt_code villagetown 

	merge 1:m state_code district_code tehsil_code villagetown using "$ec/2013/stata/Temp/EC6A_ST`i'.dta"
	drop if _merge == 1
	drop _merge

	rename vt_name villagetown_name

	compress *
	save "$ec/2013/stata/ec_2013_st_`i'.dta", replace
	clear
}


foreach i of numlist 10/27 29/35{
	import excel using "$ec/2013/raw/location codes/ST`i'.xlsx", firstrow case(lower) clear
	destring state_code district_code tehsil_code vt_code ward_code, replace
	duplicates drop state_code district_code tehsil_code vt_code, force
	rename vt_code villagetown 

	merge 1:m state_code district_code tehsil_code villagetown using "$ec/2013/stata/Temp/EC6A_ST`i'.dta"
	drop if _merge == 1
	drop _merge

	rename vt_name villagetown_name

	compress *
	save "$ec/2013/stata/ec_2013_st_`i'.dta", replace
	clear
}


// state 36 and 37 were missing location files
// create location files based on state 28
insheet using "$ec/2013/docs/ap_split_districtcodes.csv", comma clear
rename * *_new

gen district_name = district_name_new
gen state_name = "ANDHRA PRADESH"

save "$ec/2013/stata/Temp/andhra_pradesh_new.dta", replace

import excel using "$ec/2013/raw/location codes/ST28.xlsx", firstrow case(lower) clear
merge m:1 state_name district_name using "$ec/2013/stata/Temp/andhra_pradesh_new.dta", nogen

tostring state_code_new, replace
destring ward_code, replace
gen district_code_new2 = string(district_code_new,"%02.0f")

drop state_code district_code_new district_code
rename (state_code_new district_code_new2) (state_code district_code)
save "$ec/2013/stata/Temp/location_st36_37.dta", replace


forval i = 36/37 {
	use "$ec/2013/stata/Temp/location_st36_37.dta", clear

	destring state_code district_code tehsil_code vt_code ward_code, replace
	duplicates drop state_code district_code tehsil_code vt_code, force
	rename vt_code villagetown 

	merge 1:m state_code district_code tehsil_code villagetown using "$ec/2013/stata/Temp/EC6A_ST`i'.dta"
	drop if _merge == 1
	drop _merge

	rename vt_name villagetown_name

	compress *
	save "$ec/2013/stata/ec_2013_st_`i'.dta", replace
	clear
}

*Tag variables as 2011  
foreach v of varlist * {
  rename `v' `v'_11
}


** DELETE OLD TEMP FILES
clear
cd "$ec/2013/stata/Temp"
local files: dir . files "*.dta"
qui foreach f in `files' {
	erase "`f'"
}
**/

}

********************************************************************************
********************************************************************************

** Step 5: COLLAPSE 1990 DATA TO VILLAGE-INDUSTRY LEVEL
{
** DELETE OLD TEMP FILES
clear
cd "$ec/1990/stata/temp"
local files: dir . files "*.dta"
qui foreach f in `files' {
	erase "`f'"
}


///read in and collapse 1990 data - save to temp
foreach i of numlist 2/9 11/31 33 {
	use "$ec/1990/stata/ec_1990_st_`i'.dta"
	
	/**generate dummy variables we want to collapse on**/
	gen electricity = 0
	replace electricity = 1 if powerfuel == "1"
	
	gen power = 0
	replace power = 1 if powerfuel != "0"
	
	gen public = 0
	replace public = 1 if ownership == 3
	
	gen private = 0 
	replace private = 1 if ownership == 1
	
	gen perennial_dummy = 0
	replace perennial_dummy = 1 if operationnature == 1
	
	gen sch_caste = 0
	replace sch_caste = 1 if socialgroup == 1
	
	gen sch_tribe = 0
	replace sch_tribe = 1 if socialgroup == 2
	
	gen employees_ag = total_of_total if agriculture_dummy == 1
	gen employees_nonag = total_of_total if agriculture_dummy == 0
	
	gen firms = 1
	
	/** collapse files to village-nic level **/
	collapse ///
	(sum) ///
		perennial = perennial_dummy ///
		agriculture = agriculture_dummy ///
		electricity = electricity ///
		power = power ///
		public = public ///
		private = private ///
		employees = total_of_total ///
		female = totalfemale ///
		male = totalmale ///
		hired_female = hired_female ///
		hired_male = hired_male ///
		sch_caste = sch_caste ///
		sch_tribe = sch_tribe ///
		firms = firms ///
	(median) ///
		median_employees = total_of_total ///
	(mean) ///
		mean_employees = total_of_total ///
		mean_employees_ag= employees_ag ///
		mean_employees_nonag = employees_nonag, ///
	by(state state_code district_name district_code villagetown nic_code)
		
	save "$ec/1990/stata/temp/1990_vn_`i'.dta", replace
	
}

/// append all collapsed files from 1990 together
clear
gen temp = "."
local vn_files : dir "$ec/1990/stata/temp" files "1990_vn*"

foreach file of local vn_files {
    append using "$ec/1990/stata/temp/`file'"
	display "done"
}

save "$ec/village_nic1990.dta", replace


** DELETE OLD TEMP FILES
clear
cd "$ec/1990/stata/temp"
local files: dir . files "*.dta"
qui foreach f in `files' {
	erase "`f'"
}

}

********************************************************************************
********************************************************************************

** Step 6: COLLAPSE 1998 DATA TO VILLAGE-INDUSTRY LEVEL
{
** DELETE OLD TEMP FILES
clear
cd "$ec/1998/stata/temp"
local files: dir . files "*.dta"
qui foreach f in `files' {
	erase "`f'"
}

forval i = 2/36 {
	use "$ec/1998/stata/ec_1998_st_`i'.dta"
	
	/**generate dummy variables we want to collapse on**/
	gen electricity = 0
	replace electricity = 1 if powerfuel_code == 1
	
	gen power = 0
	replace power = 1 if powerfuel_code != 0
	
	gen public = 0
	replace public = 1 if ownership_code == 4
	
	gen private = 0 
	replace private = 1 if ownership_code == 1 | ownership_code == 2
	
	gen sch_caste = 0
	replace sch_caste = 1 if socialgroup_code == 6 | socialgroup_code == 7
	
	gen sch_tribe = 0
	replace sch_tribe = 1 if socialgroup_code == 8 | socialgroup_code == 9
	
	gen employees_ag = total_of_total if agriculture_dummy == 1
	gen employees_nonag = total_of_total if agriculture_dummy == 0
	
	gen inst_finance = 0
	replace inst_finance = 1 if financesource == "3"
	
	gen female_owner = 0
	replace female_owner = 1 if socialgroup_code == 1 | ///
								socialgroup_code == 3 | ///
								socialgroup_code == 6 | ///
								socialgroup_code == 8
	
	gen registered = 1
	replace registered = 0 if registrationcode1 == 0
	
	gen firms = 1
	
	rename village_code villagetown
	
	/** collapse files to village-nic level **/
	collapse ///
	(sum) ///
		perennial = perennial_dummy ///
		agriculture = agriculture_dummy ///
		electricity = electricity ///
		power = power ///
		public = public ///
		private = private ///
		employees = total_of_total ///
		female_adult = totaladult_female ///
		male_adult = totaladult_male ///
		male_child = totalchild_male ///
		female_child = totalchild_female ///
		hired_male_adult = hired_male ///
		hired_female_adult = hired_female ///
		hired_male_child = hired_malechild ///
		hired_female_child = hired_femalechild ///
		sch_caste = sch_caste ///
		sch_tribe = sch_tribe ///
		inst_finance = inst_finance ///
		female_owner = female_owner ///
		registered = registered ///
		firms = firms ///
	(median) ///
		median_employees = total_of_total ///
	(mean) ///
		mean_employees = total_of_total ///
		mean_employees_ag= employees_ag ///
		mean_employees_nonag = employees_nonag, ///
	by(state state_code district_code tehsil_code ///
		villagetown villagetown_name nic_code id)
		
	
	gen female = female_adult + female_child
	gen male = male_adult + male_child
	gen hired_female = hired_female_adult + hired_female_child
	gen hired_male = hired_male_adult + hired_male_child
	
	save "$ec/1998/stata/temp/1998_vn_`i'.dta", replace

}


/// append all collapsed files from 1998 together
clear
gen temp = "."
local vn_files : dir "$ec/1998/stata/temp" files "1998_vn*"

foreach file of local vn_files {
    append using "$ec/1998/stata/temp/`file'"
}

drop temp 
save "$ec/village_nic1998.dta", replace

** DELETE OLD TEMP FILES
clear
cd "$ec/1998/stata/temp"
local files: dir . files "*.dta"
qui foreach f in `files' {
	erase "`f'"
}

}

********************************************************************************
********************************************************************************

** Step 7: COLLAPSE 2005 DATA TO VILLAGE-INDUSTRY LEVEL
{

** DELETE OLD TEMP FILES
clear
cd "$ec/2005/stata/temp"
local files: dir . files "*.dta"
qui foreach f in `files' {
	erase "`f'"
}

foreach i of numlist 1/35 {
use "$ec/2005/stata/ec_2005_st_`i'.dta"
	
	/**generate dummy variables we want to collapse on**/
	gen electricity = 0
	replace electricity = 1 if powerfuel_code == 1
	
	gen power = 0
	replace power = 1 if powerfuel_code != 0
	
	gen public = 0
	replace public = 1 if ownership_code == 4
	
	gen private = 0 
	replace private = 1 if ownership_code == 1 | ownership_code == 2
	
	gen sch_caste = 0
	replace sch_caste = 1 if socialgroup_code == 2 | socialgroup_code == 6
	
	gen sch_tribe = 0
	replace sch_tribe = 1 if socialgroup_code == 1 | socialgroup_code == 5
	
	gen employees_ag = total_of_total if agriculture_dummy == 1
	gen employees_nonag = total_of_total if agriculture_dummy == 0
	
	gen inst_finance = 0
	replace inst_finance = 1 if financesource == "3"
	
	gen female_owner = 0
	replace female_owner = 1 if socialgroup_code == 1 | ///
								socialgroup_code == 2 | ///
								socialgroup_code == 3 | ///
								socialgroup_code == 4
	
	gen registered = 1
	replace registered = 0 if registrationcode1 == 0
	
	gen firms = 1
	
	rename villagetown_code villagetown
	
	/** collapse files to village-nic level **/
	collapse ///
	(sum) ///
		perennial = perennial_dummy ///
		agriculture = agriculture_dummy ///
		electricity = electricity ///
		power = power ///
		public = public ///
		private = private ///
		employees = total_of_total ///
		female_adult = totaladult_female ///
		male_adult = totaladult_male ///
		male_child = totalchild_male ///
		female_child = totalchild_female ///
		nonhired_male_adult = nonhired_male ///
		nonhired_female_adult = nonhired_female ///
		nonhired_male_child = nonhiredchild_male ///
		nonhired_female_child = nonhiredchild_male ///
		sch_caste = sch_caste ///
		sch_tribe = sch_tribe ///
		inst_finance = inst_finance ///
		female_owner = female_owner ///
		registered = registered ///
		firms = firms ///
	(median) ///
		median_employees = total_of_total ///
	(mean) ///
		mean_employees = total_of_total ///
		mean_employees_ag= employees_ag ///
		mean_employees_nonag = employees_nonag, ///
	by(state state_code district_name district_code tehsil_name tehsil_code ///
		villagetown villagetown_name nic_code)
		
	
	gen female = female_adult + female_child
	gen male = male_adult + male_child
	gen nonhired_female = nonhired_female_adult + nonhired_female_child
	gen nonhired_male = nonhired_male_adult + nonhired_male_child
	gen hired_female = female_adult + female_child - nonhired_female_child - nonhired_female_adult
	gen hired_male = male_adult + male_child - nonhired_male_child - nonhired_male_adult
	save "$ec/2005/stata/temp/2005_vn_`i'.dta", replace

}


* append all collapsed files from 2005 together
clear
gen temp = "."
local vn_files : dir "$ec/2005/stata/temp" files "2005_vn*"

foreach file of local vn_files {
    append using "$ec/2005/stata/temp/`file'"
}

drop temp 
destring villagetown, replace



****************************label variables ***********************************
label var state_code "State code (match with 2001 census)"
label var district_name "District Name"
label var district_code "District code"
label var tehsil_name "Subdistrict name"
label var tehsil_code "Subdistrict code"
label var nic_code "Major NIC activity - 2004 code"
label var villagetown "Village/town code"
label var state "State name"
label var villagetown_name "Village/town name"
label var perennial "# of firms with perennial activity"
label var public "# of public firms"
label var private "# of private firms"
label var employees "# of employees"
label var median_employees "median # of employees per firm "
label var mean_employees "mean # of employees per firm"
label var mean_employees_ag "mean # of employees per firm in agricultural firms"
label var mean_employees_nonag "mean # of employees per firm in non-agricultural firms"
label var female "number of female employees"
label var male "number of male employees"
label var female_adult "# of female adult employees"
label var male_adult "# of male adult employees"
label var male_child "# of male child employees"
label var female_child "# of female child employees"
label var hired_female "# of paid female employees"
label var hired_male "# of paid male employees"
label var nonhired_male "Male, not hired"
label var nonhired_female "Female, not hired"
label var nonhired_male_adult "# of nonpaid adult male employees"
label var nonhired_male_child "# of nonpaid child male employees"
label var nonhired_female_adult "# of nonpaid adult female employees"
label var nonhired_female_child "# of nonpaid child female employees"
label var sch_caste "# of firms where owner's social group is scheduled caste"
label var sch_tribe "# of firms where owner's social group is scheduled tribe"
label var inst_finance "# of firms who borrowed money from an institution"
label var female_owner "# of firms where owner is a female"
label var firms "# of firms total"
label var agriculture "# of firms with agricultural acitivty"
label var electricity "# of firms that use electricity as their power source"
label var power "# of firms that use any fuel for a power source"
label var registered "# of registered firms"

destring *code*, replace
compress *
save "$ec/2005/stata/temp/village_nic.dta", replace



**************************Add in PCA 2001 ID*******************************
* connect 2005 economic census to 2001 PCA data
* read in pca location codes
use "$pca/pca_census01_names.dta", clear
rename st_code state_code
rename dt_code district_code
rename bk_code_pca tehsil_code
rename vi_code villagetown
rename state state_pca
rename district district_pca
rename block block_pca
rename village villagetown_name

ds, has(type string) 
foreach v in `r(varlist)' { 
  replace `v' = itrim(trim(upper(`v')))
  replace `v' = trim(itrim(subinstr(subinstr(`v', ")", "", .), "(", "", .)))
}	
save "$dirpath_temp/pca01.dta", replace

* slim down economic census data
use "$ec/2005/stata/temp/village_nic.dta", clear
keep state_code - villagetown_name
drop nic_code
duplicates drop
gen ec05_id = _n

ds, has(type string) 
foreach v in `r(varlist)' { 
  replace `v' = itrim(trim(upper(`v')))
  replace `v' = trim(itrim(subinstr(subinstr(`v', ")", "", .), "(", "", .)))
}	
save "$ec/2005/stata/temp/ec05.dta", replace
merge 1:m state_code district_code tehsil_code villagetown using "$ec/2005/stata/temp/pca01.dta"

* save ones that are matched
keep if _merge == 3
drop _merge 
save "$ec/2005/stata/temp/ec05_pca_matched.dta", replace

******************** match on village names on unmatched ones*****************
* anti join villages that are matchable by codes 
use "$ec/2005/stata/temp/pca01.dta", clear
merge 1:1 pca01_id using "$ec/2005/stata/temp/ec05_pca_matched.dta", keepusing(pca01_id)
keep if _merge == 1
drop _merge
rename state_pca state
rename district_pca district_name
rename block_pca tehsil_name
save "$ec/2005/stata/temp/pca01_unmatched.dta", replace

use "$ec/2005/stata/temp/ec05.dta", clear
merge 1:1 ec05_id using "$ec/2005/stata/temp/ec05_pca_matched.dta", keepusing(ec05_id)
keep if _merge == 1
drop _merge
save "$ec/2005/stata/temp/ec05_unmatched.dta", replace


* reclink on unmatched villages
foreach i of numlist 1/3 5/6 8/24 27/29 32/33 {
	use "$ec/2005/stata/temp/pca01_unmatched.dta", clear
	keep if state_code == `i'
	save "$ec/2005/stata/temp/pca01_state_`i'.dta", replace
}

* now subset pca 1991 and match with subsetted pca 2001 data
foreach i of numlist 1/3 5/6 8/24 27/29 32/33 {
	use "$ec/2005/stata/temp/ec05_unmatched.dta", clear
	keep if state_code == `i'
	
	reclink state_code district_name tehsil_name villagetown_name ///
	using "$ec/2005/stata/temp/pca01_state_`i'.dta", orblock(state_code) ///
	idmaster(ec05_id) idusing(pca01_id) gen(match_score) minscore(.95)
	rename _merge vt_merge
	
	* separate match successes from match failures
	preserve
	keep if vt_merge == 3
	save "$ec/2005/stata/temp/pca_ec05_st_`i'_match.dta", replace
	
	restore
	keep if vt_merge != 3
	save "$ec/2005/stata/temp/pca_ec05_`i'_nomatch.dta", replace
	
	display `i'
}

* append reclink matches and clean
use "$ec/2005/stata/temp/pca_ec05_st_1_match.dta", clear
foreach i of numlist 2/3 5/6 8/24 27/29 32/33 {
	append using "$ec/2005/stata/temp/pca_ec05_st_`i'_match.dta"
}

gen neg_score = -match_score
sort ec05_id neg_score
bysort ec05_id: gen top_match = _n == 1
keep if top_match == 1

sort pca01_id neg_score
bysort pca01_id: gen ptop_match = _n == 1
keep if ptop_match == 1
drop U* match_score vt_merge neg_score top_match ptop_match 

* append to villages matched by code
append using "$ec/2005/stata/temp/ec05_pca_matched.dta"

* merge back in with ec05 data
keep state_code district_code tehsil_code villagetown pca01_id
joinby state_code district_code tehsil_code villagetown using ///
	"$ec/2005/stata/temp/village_nic.dta", unmatched(using)

compress *
save "$ec/village_nic2005.dta", replace

** DELETE TEMP FILES
clear
cd "$ec/2005/stata/temp"
local files: dir . files "*.dta"
qui foreach f in `files' {
	erase "`f'"
}	
}

********************************************************************************
********************************************************************************

** Step 8: COLLAPSE 2013 DATA TO VILLAGE-INDUSTRY LEVEL
{

foreach i of numlist 1/27 29/37{
	use "$ec/2013/stata/ec_2013_st_`i'.dta"
	
	* generate dummy variables we want to collapse on

	gen public = 0
	replace public = 1 if ownership_code == 4
	
	gen private = 0 
	replace private = 1 if ownership_code == 1 | ownership_code == 2
	
	gen sch_caste = 0
	replace sch_caste = 1 if socialgroup_code == 2 | socialgroup_code == 6
	
	gen sch_tribe = 0
	replace sch_tribe = 1 if socialgroup_code == 1 | socialgroup_code == 5
	
	gen inst_finance = 0
	replace inst_finance = 1 if financesource == "3"
	
	gen female_owner = 0
	replace female_owner = 1 if socialgroup_code == 1 | ///
								socialgroup_code == 2 | ///
								socialgroup_code == 3 | ///
								socialgroup_code == 4
	
	gen firms = 1
	
	rename state_name state
	
	* collapse files to village-nic level 
	collapse ///
	(sum) ///
		perennial = perennial_dummy ///
		public = public ///
		private = private ///
		employees = total_of_total ///
		hired_male = hired_male ///
		hired_female = hired_female ///
		nonhired_male = nonhired_male ///
		nonhired_female = nonhired_female ///
		sch_caste = sch_caste ///
		sch_tribe = sch_tribe ///
		inst_finance = inst_finance ///
		female_owner = female_owner ///
		firms = firms ///
	(median) ///
		median_employees = total_of_total ///
	(mean) ///
		mean_employees = total_of_total, ///
	by(state district_name state_code district_code tehsil_name tehsil_code ///
		villagetown villagetown_name nic_code)
		
	
	gen female = hired_female + nonhired_female
	gen male = hired_male + nonhired_male

	save "$ec/2013/stata/temp/2013_vn_`i'.dta", replace
}


/// append all collapsed files from 2013 together
clear
gen temp = "."
local vn_files : dir "$ec/2013/stata/temp" files "2013_vn*"

foreach file of local vn_files {
    append using "$ec/2013/stata/temp/`file'"
}

drop temp 


****************************label variables ***********************************
label var state_code "State code (match with 2001 census)"
label var district_name "District Name"
label var district_code "District code"
label var tehsil_name "Subdistrict name"
label var tehsil_code "Subdistrict code"
label var nic_code "Major NIC activity - 2004 code"
label var villagetown "Village/town code"
label var state "State name"
label var villagetown_name "Village/town name"
label var perennial "# of firms with perennial activity"
label var public "# of public firms"
label var private "# of private firms"
label var employees "# of employees"
label var median_employees "median # of employees per firm "
label var mean_employees "mean # of employees per firm"
label var female "number of female employees"
label var male "number of male employees"
label var hired_female "# of paid female employees"
label var hired_male "# of paid male employees"
label var nonhired_male "Male, not hired"
label var nonhired_female "Female, not hired"
label var sch_caste "# of firms where owner's social group is scheduled caste"
label var sch_tribe "# of firms where owner's social group is scheduled tribe"
label var inst_finance "# of firms who borrowed money from an institution"
label var female_owner "# of firms where owner is a female"
label var firms "# of firms total"


// change all string characters to upper case
ds, has(type string) 
foreach v in `r(varlist)' { 
  replace `v' = itrim(trim(upper(`v')))
  replace `v' = trim(itrim(subinstr(subinstr(`v', ")", "", .), "(", "", .)))
}	

compress *
save "$ec/2013/stata/temp/village_nic.dta", replace

******************************Join with pca 2011 codes****************************
* slim down to just location ID for matching with PCA
keep state_code state district_name tehsil_name villagetown_name
duplicates drop
drop if state == ""

// change all string characters to upper case
ds, has(type string) 
foreach v in `r(varlist)' { 
  replace `v' = itrim(trim(upper(`v')))
  replace `v' = trim(itrim(subinstr(subinstr(`v', ")", "", .), "(", "", .)))
}	

gen ec13_id = _n
save "$ec/2013/stata/temp/ec13.dta", replace

*******************************read in pca 2011*************************************
use "$pca/pca_census11_names.dta", clear
drop no_hh - tot_f
rename st_code11 state_code
rename district11 district_name
rename block11 tehsil_name_pca
rename village11 villagetown_name
duplicates drop pca11_id, force

// change all string characters to upper case
ds, has(type string) 
foreach v in `r(varlist)' { 
  replace `v' = itrim(trim(upper(`v')))
  replace `v' = trim(itrim(subinstr(subinstr(`v', ")", "", .), "(", "", .)))

}	

save "$ec/2013/stata/temp/pca11.dta", replace


*******************************reclink****************************************
foreach i of numlist 1/3 5/6 8/24 27/29 32/33 {
	use "$ec/2013/stata/temp/pca11.dta", clear
	keep if state_code == `i'
	save "$ec/2013/stata/temp/pca11_state_`i'.dta", replace
}

// now subset pca 1991 and match with subsetted pca 2001 data
foreach i of numlist 1/3 5/6 8/24 27/29 32/33 {
	use "$ec/2013/stata/temp/ec13.dta", clear
	keep if state_code == `i'
	
	reclink state_code district_name villagetown_name ///
	using "$ec/2013/stata/temp/pca11_state_`i'.dta", orblock(state_code) ///
	idmaster(ec13_id) idusing(pca11_id) gen(match_score) minscore(.95)
	rename _merge vt_merge
	
	// separate match successes from match failures
	preserve
	keep if vt_merge == 3
	save "$ec/2013/stata/temp/vt_st_`i'_match.dta", replace
	
	restore
	keep if vt_merge != 3
	save "$ec/2013/stata/temp/vt_st_`i'_nomatch.dta", replace
	
	display `i'
}


// append reclink matches and clean
use "$ec/2013/stata/temp/vt_st_2_match.dta", clear
foreach i of numlist 2/3 5/6 8/24 27/29 32/33 {
	append using "$ec/2013/stata/temp/vt_st_`i'_match.dta"
}

* make sure there is a 1:1 match between pca and ec villages
gen neg_score = -match_score
sort ec13_id neg_score
bysort ec13_id: gen top_match = _n == 1
keep if top_match == 1

sort pca11_id neg_score
bysort pca11_id: gen ptop_match = _n == 1
keep if ptop_match == 1
drop U* match_score vt_merge neg_score top_match ptop_match ec13_id

* join back with main ec 2013 dataset
keep state_code district_name tehsil_name villagetown_name pca11_id
joinby state_code district_name tehsil_name villagetown_name using ///
	"$ec/2013/stata/temp/village_nic.dta", unmatched(using)
drop _merge
	
compress *
save "$ec/village_nic2013.dta", replace

/
** DELETE OLD TEMP FILES
clear
cd "$ec/2013/stata/temp"
local files: dir . files "*.dta"
qui foreach f in `files' {
	erase "`f'"
}

}

********************************************************************************
********************************************************************************
